package com.qianguo.buyer.widget.dropdownmenu;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.qianguo.buyer.R;
import java.util.ArrayList;
import java.util.List;


/**
 * 下拉条件列表选择布局
 * Created by guxun on 16/2/29.
 */
public class DropDownFilterListView extends LinearLayout {
    /**
     * 特殊条件“全部”对应的code
     */
    private int mNearbyConCode = -100;
    /**
     * 特殊条件“附近”对应的code
     */
    private int mAllConCode = 0;
    private int dividerColor = 0xffcccccc;//分割线颜色
    private int textSelectedColor = 0xff890c85;
    private ListView mLeftListView;
    private ListView mRightListView;
    private View mVerticalView;//双列表模式下的分割线
    private boolean isSingleFilterMode = false;//默认双列表模式

    private int mLeftWeight = 3;
    private int mRightWeight = 4;
    private List<BaseMenuFilterItem> mLeftList = new ArrayList<>();
    private List<MenuListFilterItem> mAllList = new ArrayList<>();
    private int[] mPosition = new int[2];//存放左右列表的点击位置

    OnConditionSelectedListener mSelectListener;
    OnDoubleConditionSelectedListener mDoubleSelectListener;

    public DropDownFilterListView(Context context) {
        super(context);
    }

    public DropDownFilterListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DropDownFilterListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownFilterListView);
        isSingleFilterMode = a.getBoolean(R.styleable.DropDownFilterListView_singleFilter, false);
        mLeftWeight = a.getInteger(R.styleable.DropDownFilterListView_leftListWeight, 3);
        mRightWeight = a.getInteger(R.styleable.DropDownFilterListView_rightListWeight, 4);
        a.recycle();
        init();


    }

    private void init() {
        initLeftListView();
        initVerticalSplitView();
        initRightListView();
        initListMode();

    }

    private void initListMode() {
        if (isSingleFilterMode)
            mRightListView.setVisibility(View.GONE);
    }

    private void initLeftListView() {
        LayoutParams paramLeft = new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, mLeftWeight);
        mLeftListView = new ListView(getContext());
        mLeftListView.setDividerHeight(0);
        mLeftListView.setLayoutParams(paramLeft);
        mLeftListView.setBackgroundColor(dividerColor);
        addView(mLeftListView);
    }

    private void initVerticalSplitView() {
        mVerticalView = new View(getContext());
        mVerticalView.setLayoutParams(new LayoutParams(dpTpPx(0.5f), ViewGroup.LayoutParams.MATCH_PARENT));
        mVerticalView.setBackgroundColor(dividerColor);
        addView(mVerticalView);
    }

    private void initRightListView() {
        LayoutParams paramRight = new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, mRightWeight);
        mRightListView = new ListView(getContext());
        mRightListView.setBackgroundColor(textSelectedColor);
        mRightListView.setDividerHeight(0);
        mRightListView.setLayoutParams(paramRight);
        addView(mRightListView);

    }

    private void initLeftListAdapter() {
        mLeftAdapter = new SingleListAdapter(getContext(), mLeftList);
        mLeftListView.setAdapter(mLeftAdapter);
        mLeftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mPosition[0] = position;

                mLeftAdapter.setCheckItem(position);
                if (isSingleFilterMode) {
                    if (mSelectListener != null)
                        mSelectListener.onFilterSelected(mLeftList.get(position), position);

                } else {
                    if (mAllList.get(position) != null && mAllList.get(position).getChildren() != null && mAllList.get(position).getChildren().size() > 0) {

//                        mRightAdapter = new SingleListAdapter(getContext(), mAllList.get(position).getChildren());
//                        mRightListView.setAdapter(mRightAdapter);
//                        mRightAdapter.notifyDataSetChanged();
                        Log.i("sss", position + "sss");
                        mRightAdapter.setListData(mAllList.get(position).getChildren());
                    }
                }
            }
        });
    }

    private void initRightListAdapter() {
        if (isSingleFilterMode) {
            throw new IllegalArgumentException("当前为筛选器为单列表模式");
        } else {
            if (mAllList.size() > 0 && mAllList.get(0).getChildren() != null) {
                mRightAdapter = new SingleListAdapter(getContext(), mAllList.get(0).getChildren());

                mRightListView.setAdapter(mRightAdapter);
                mRightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mPosition[1] = position;
                        mRightAdapter.setCheckItem(position);
                        if (mDoubleSelectListener != null && mAllList.get(mPosition[0]) != null && mAllList.get(mPosition[0]).getChildren() != null) {
                            mDoubleSelectListener.onFilterSelected(mAllList.get(mPosition[0]).getChildren().get(position), mPosition);
                        }
                    }
                });
            }
        }


    }

    public int dpTpPx(float value) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm) + 0.5);
    }

    SingleListAdapter mLeftAdapter;
    SingleListAdapter mRightAdapter;

    class SingleListAdapter extends BaseAdapter {

        private Context context;
        private List<BaseMenuFilterItem> list = new ArrayList<>();
        private int checkItemPosition = -1;

        public void setCheckItem(int position) {
            checkItemPosition = position;
            notifyDataSetChanged();
        }

        public int getCheckItem() {
            return checkItemPosition;
        }

        public void setListData(List<BaseMenuFilterItem> aList) {
            list.clear();
            list.addAll(aList);
            resetCheckItem();
            notifyDataSetChanged();
        }

        private void resetCheckItem() {
            checkItemPosition = -1;
        }


        public SingleListAdapter(Context context, List<BaseMenuFilterItem> list) {
            this.context = context;
            if (list != null) {
                this.list.addAll(list);
            }
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView != null) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_default_drop_down, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }
            fillValue(position, viewHolder);
            return convertView;
        }

        private void fillValue(int position, ViewHolder viewHolder) {
            viewHolder.mText.setText(list.get(position).getsName());
            if (checkItemPosition != -1) {
                if (checkItemPosition == position) {
                    viewHolder.mText.setTextColor(context.getResources().getColor(R.color.drop_down_selected));
                    viewHolder.mText.setBackgroundResource(R.color.check_bg);
                } else {
                    viewHolder.mText.setTextColor(context.getResources().getColor(R.color.drop_down_unselected));
                    viewHolder.mText.setBackgroundResource(R.color.white);
                }
            } else {
                viewHolder.mText.setTextColor(context.getResources().getColor(R.color.drop_down_unselected));
                viewHolder.mText.setBackgroundResource(R.color.white);
            }
        }

        class ViewHolder {
            TextView mText;

            ViewHolder(View view) {
                mText = (TextView)view.findViewById(R.id.tv_filter_item);
            }
        }

    }

    /**
     * 单列表筛选模式
     *
     * @param list
     * @param listener
     */
    public void showSingleListFilter(List<BaseMenuFilterItem> list, OnConditionSelectedListener listener) {
        isSingleFilterMode = true;
        mLeftList.clear();
        mLeftList.addAll(list);
        mSelectListener = listener;
        initLeftListAdapter();
    }


    /**
     * 双列表筛选模式
     *
     * @param list
     * @param listener
     */
    public void showDoubleListFilter(List<MenuListFilterItem> list, OnDoubleConditionSelectedListener listener) {
        isSingleFilterMode = false;
        mAllList.clear();
        mAllList.addAll(list);
        mLeftList.clear();
        for (MenuListFilterItem listItem : list) {
            mLeftList.add(listItem);
        }
        mDoubleSelectListener = listener;
        Log.i("mmm", mAllList.size() + "" + mLeftList.size());
        initRightListAdapter();
        initLeftListAdapter();
    }


    /**
     * 单列表条件回调接口
     */
    public interface OnConditionSelectedListener {
        /**
         * @param item
         * @param a    Position
         */
        void onFilterSelected(BaseMenuFilterItem item, int a);

    }

    /**
     * 多条件回调接口
     */
    public interface OnDoubleConditionSelectedListener {
        /**
         * @param item
         * @param a    a[0] groupPosition
         *             a[1]:childPosition
         */
        void onFilterSelected(BaseMenuFilterItem item, int[] a);

    }


}
